#cnu.py

import os
from bs4 import BeautifulSoup
import requests
import json
import re

url  = 'http://www.cnu.cc/inspirationPage/recent-118?page='

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}

img_url = 'http://img.cnu.cc/uploads/images/920/'

save_path = 'C:/python_project/cnu'


def saveFile(file_name,img_body):
	f = open(file_name,'ab')
	f.write(img_body)
	print("图片"+file_name+"保存成功")
	f.close()

#创建文件夹
def createFile(file_path):
	if os.path.exists(file_path) is False:
		os.makedirs(file_path)
	os.chdir(file_path)

def main():
	for x in range(1,14):
		res = requests.get(url+str(x),headers)
		soup = BeautifulSoup(res.text, 'html.parser')

		a_link =soup.find('div', class_ = 'grid').find_all('a',target= '_blank')
		#print(a_link)
		for x in a_link:
			#print(x)
			#创建文件夹
			name = x.text.strip();
			name = name.replace(' ','')
			
			cop = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]") # 匹配不是中文、大小写、数字的其他字符
			name = cop.sub('',name)

			createFile(save_path+'/'+name)
			print('当前执行下载文件夹：'+name)
			#打开页面
			href = x.attrs['href']
			print('访问页面地址:'+href)

			res_1 = requests.get(href,headers)
			soup_1 = BeautifulSoup(res_1.text,'html.parser')


			imgList = soup_1.find('div', id='imgs_json')

			#print(imgList.content)
			
			list_img = json.loads(imgList.text)
			for x in list_img:
				#print(x['img'])
				#获取图片名称
				img_object = requests.get(img_url+x['img'],headers)
				#print(img_object)
				array = x['img'].split('/')
				file_name = array[len(array)-1]
				#file_name = file_name[0:file_name.rfind('?', 1)]
				#保存图片
				saveFile(file_name,img_object.content)



if __name__ == '__main__':
	main()
